package com.coding.kan_yi_dao.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import com.coding.kan_yi_dao.common.Result;
import com.coding.kan_yi_dao.entity.GoodsLike;
import com.coding.kan_yi_dao.entity.User;
import com.coding.kan_yi_dao.mapper.UserMapper;
import com.coding.kan_yi_dao.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 砍一刀，不买就砍你一刀
 * @since 2023-09-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result go_front_login(User user) {
        String md5password = new String(DigestUtil.md5Hex(user.getUserPassword()));
        User user1 = query().eq("login_name", user.getLoginName())
                .eq("user_password", md5password)
                .one();
        if (user1 != null) {
            // 5、随机生成token（编写算法，生成token永不重复）
            String token = UUID.randomUUID().toString(true);
            // 6、将用户的信息存储到redis当中，以便于其他业务进入到后台的时候做登录校验
            redisTemplate.opsForValue().set("login:user:" + token, JSON.toJSONString(user1), 30L, TimeUnit.MINUTES);
            return Result.ok(token);
        }
        return Result.fail("账号或密码错误");
    }

    @Override
    public Result go_register(User user) {
        User user1 = query().eq("login_name", user.getLoginName()).one();
        if (user1 == null) {
            String md5password = new String(DigestUtil.md5Hex(user.getUserPassword()));
            user.setUserPassword(md5password);
            int a = baseMapper.insert(user);
            if (a == 0) {
                return Result.fail("注册失败");
            }
            return Result.ok(a);
        }
        return Result.fail("账号重复 请重新输入");
    }

    @Override
    public Result go_update(Integer userId, String userPassword, String new_userPassword) {
        String md5password = new String(DigestUtil.md5Hex(userPassword));
        User user = query().eq("user_id", userId).one();
        if (md5password.equals(user.getUserPassword())) {
            User user1 = new User();
            String md5password1 = new String(DigestUtil.md5Hex(new_userPassword));
            user1.setUserPassword(md5password1);
            user1.setUserId(userId);
            Boolean a = updateById(user1);
            return Result.ok(a);
        }
        return Result.fail("旧密码错误");
    }

    @Override
    public Result go_select(User user) {
        if (user.getUserId() == null) {
            return Result.ok(list());
        }
        return Result.ok(baseMapper.selectById(user));
    }

    //锁定账户
    @Override
    public Result go_update(List<Integer> ids) {
        List<User> users = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            User user = new User();
            user.setUserId(ids.get(i));
            user.setLockedFlag(1);
            users.add(user);
        }
        return Result.ok(updateBatchById(users));
    }

    //解除账户
    @Override
    public Result go_updates(List<Integer> ids) {
        List<User> users = new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            User user = new User();
            user.setUserId(ids.get(i));
            user.setLockedFlag(0);
            users.add(user);
        }
        return Result.ok(updateBatchById(users));
    }

    @Override
    public Result go_updatess(User user) {
        return Result.ok(updateById(user));
    }
}
